<template>
  <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="40px" class="demo-ruleForm">
    <el-form-item label="帐号" prop="username">
      <el-input v-model="ruleForm.username" type="text" />
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="ruleForm.password" type="password" autocomplete="off" />
    </el-form-item>
    <el-form-item label="角色">
      <el-select v-model="roleIdx" class="m-2" placeholder="请选择帐号的角色" size="large">
        <el-option v-for="(item, index) in roleList" :key="item.objectId" :label="item.roleName" :value="index" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="handleAdd">新增帐号</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { roleGet, userReg } from '../../api/user';
export default {
  data() {
    return {
      ruleForm: {
        username: "周洲",
        password: "ZZ134679",
      },
      roleList: [],
      roleIdx: ''
    }
  },
  created() {
    roleGet().then(res => {
      this.roleList = res.data.results
      console.log(res.data.results)
    })
  },
  methods: {
    handleAdd() {
      let { objectId: roleId, roleName, roleMenu } = this.roleList[this.roleIdx]
      userReg({ roleId, roleName, roleMenu, ...this.ruleForm })
      localStorage.setItem('vue-admin-userinfo', JSON.stringify(roleMenu))
    }
  }
}
</script>

<style lang="scss" scoped>
.demo-ruleForm {
  width: 500px;
  // margin-left: 100px;
}
</style>